﻿using System;
using GameEditor.View;
using GameEngine.Model.Data;
using GameEngine.View;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using SilverlightClientEditor.View.Components;
using SilverlightShowCase.View.Components;
using System.Windows.Controls;
using System.IO;
using SilverlightClientEditor;
using GameEngine.Clients.Silverlight.View.Components;
using System.Windows;
using GameEngine.Clients.Silverlight;
using GameEngine.Model.Data.implementation;

namespace SilverlightShowCase.Commands
{
    /// <summary>
    /// 
    /// </summary>
    public class Page_GamesEditor_In_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            MenuMediator<string, GamesEditorMenu> mediator = new MenuMediator<string, GamesEditorMenu>(
                MyFacade.MyGamesEditorMenuMediatorName,
                new GamesEditorMenu(),
                MyFacade.MY_PAGE_GAMESEDITOR_OUT_COMMAND);
            Facade.RegisterMediator(mediator);
            MyFacadeHelper.DesktopNavigate(mediator.ViewComponent);
        }
    }

    public class Page_GamesEditor_Out_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            var mediator = Facade.RetrieveMediator(MyFacade.MyGamesEditorMenuMediatorName) as GamePropertiesEditor_MenuMediator<GamePropertiesEditorUC, int>;
            Facade.RemoveMediator(MyFacade.MyGamesEditorMenuMediatorName);
            switch (notification.Body as string)
            {
                case GamesEditorMenu.BACKBUTTON:
                    MyFacade.Instance.SendNotification(MyFacade.MY_PAGE_MAIN_IN_COMMAND);
                    break;
                case GamesEditorMenu.NEWGAMEBUTTON:
                    {
                        var gameId = new GameVOId<int>(MyModelFacade.Instance.CreateNewId());
                        var game = new GameVO<int>(gameId);
                        
                        //TODO tirar e fazer um xml default
                        ShapeVO blackCube = new ShapeVO(new ShapeVOId("black cube"), new ShapeTypeVOId(ItemUC.ShapeTypeCube))
                        { // cube.Id
                            Position = new MapEngine.Point3d(),
                            TypeModifiers = "0,0,5,10,10,10,0,0,0,255"
                        };
                        ViewVO<int> sampleView = new ViewVO<int>(new ViewVOId<int>(1,"blackCube"));
                        sampleView.Shapes[IViewVOConstants.DefaultState].Add(blackCube.Id, blackCube);
                        game.Views.Add(sampleView.Id, sampleView);
            
                        var thingType = new ThingTypeVO<int>(new ThingTypeVOId<int>(SilverlightModelFacade.Instance.CreateNewId(),"blackCube"));
                        thingType.ViewId = sampleView.Id;
                        game.ThingTypes.Add(thingType.Id, thingType);

                        var level = new LevelVO<int>(new LevelVOId<int>(MyModelFacade.Instance.CreateNewId(),"lvl1"));
                        var map = new MapVO("teste") { 
                            Size = new Point(10,10),
                            Type = GameEngine.View.Components.TileType.IsometricSquare
                        };
                        var world = new WorldVO<int>(new WorldVOId("world")) { 
                            Map= map,
                        };
                        level.Worlds.Add(world);
                        game.Levels.Add(level.Id, level);
                        game.LevelOrder.Add(0, level.Id);
                        game.CurrentLevel = 0;

                        var dataProxy = MyFacadeHelper.CreateGame(game);


                        SendNotification(MyFacade.PAGE_EDITOREDITGAME_IN_COMMAND, dataProxy);

                        //var gameEditor = SilverlightEditorFacade.Instance.Start(game, MyFacade.MY_PAGE_GAMESEDITOR_IN_COMMAND);
                        //gameEditor.OnSaveButtonClick += delegate()
                        //{
                        //    SaveFileDialog sd = new SaveFileDialog();
                        //    if (sd.ShowDialog().Value)
                        //    {
                        //        string output = game.Serialize();
                        //        TextWriter w = new StreamWriter(sd.OpenFile());
                        //        w.Write(output);
                        //        w.Flush();
                        //        w.Close();
                        //    }
                        //};
                        //MyFacadeHelper.DesktopNavigate(gameEditor);
                    }
                    break;
                case GamesEditorMenu.EDITGAMEBUTTON:
                    {
                        OpenFileDialog ofd = new OpenFileDialog();
                        if (ofd.ShowDialog().Value)
                        {
                            TextReader w = new StreamReader(ofd.File.OpenRead());
                            string gameSerialized = w.ReadToEnd();
                            w.Close();
                            var game = new GameVO<int>();
                            game.DeSerialize(gameSerialized);
                            MyFacadeHelper.CreateGame(game);
                            var gameDataProxy = MyModelFacade.Helper.GetGameDataProxy(game);

                            SendNotification(MyFacade.PAGE_EDITOREDITGAME_IN_COMMAND, gameDataProxy);
                            //SendNotification(SilverlightEditorFacade.EDITORSTARTUP_COMMAND, gameDataProxy);
                        }
                    }
                    break;
            }
        }

        

        //public IGameVO<int> CreateGame()
        //{
        //    //TODO meter isto em config file

        //    ///Game
        //    //IGameVO<int> game = new GameVO<int>(new GameVOId("MyGame"));

        //    //#region resources
        //    ////game.Resources.Add("maleandfemalefinal.png", new Image() { Source = new BitmapImage(new Uri("/Resources/maleandfemalefinal.png",UriKind.Relative)) });
        //    ////game.Resources.Add("maleandfemalefinal.png", new Image() { Source = new BitmapImage(new Uri("/Resources/tux.png", UriKind.Relative)) });
        //    ////Application.Current.Resources.Add("maleandfemalefinal.png", new Image() { Source = new BitmapImage(new Uri("/Resources/maleandfemalefinal.png", UriKind.Relative)) });
        //    //#endregion

        //    ////Add Views
        //    //#region shapes
        //    //ShapeVO redCube = new ShapeVO(new ShapeVOId("red cube"), new ShapeTypeVOId(ItemUC.ShapeTypeCube))
        //    //{ // cube.Id
        //    //    Position = new MapEngine.Point3d(),
        //    //    //              position  size      color
        //    //    TypeModifiers = "0,0,7.5, 20,20,15, 255,0,0,255" //TODO Cube.Color = Red).getModifiers
        //    //};

        //    //ShapeVO yellowCube = new ShapeVO(new ShapeVOId("yellow cube"), new ShapeTypeVOId(ItemUC.ShapeTypeCube))
        //    //{ // cube.Id
        //    //    Position = new MapEngine.Point3d(),
        //    //    TypeModifiers = "0,0,1, 30,30,1, 255,255,0,255" //TODO Cube.Color = Red).getModifiers
        //    //};

        //    //ShapeVO greenSphere = new ShapeVO(new ShapeVOId("green sphere"), new ShapeTypeVOId(ItemUC.ShapeTypeElipse))
        //    //{ // cube.Id
        //    //    Position = new MapEngine.Point3d(),
        //    //    TypeModifiers = "0,0,0,5,5,0,255,0,255" //TODO Cube.Color = Red).getModifiers
        //    //};

        //    //ShapeVO brownCube = new ShapeVO(new ShapeVOId("brown cube"), new ShapeTypeVOId(ItemUC.ShapeTypeCube))
        //    //{ // cube.Id
        //    //    Position = new MapEngine.Point3d(),
        //    //    TypeModifiers = "0,0,2.5,5,5,10,139,69,19,255" //TODO Cube.Color = Red).getModifiers
        //    //};

        //    //ShapeVO arvoreGreenSphere = new ShapeVO(new ShapeVOId("green sphere"), new ShapeTypeVOId(ItemUC.ShapeTypeElipse))
        //    //{ // cube.Id
        //    //    Position = new MapEngine.Point3d(new Point(), 2),
        //    //    TypeModifiers = "0,0,15,20,20,0,255,0,150" //TODO Cube.Color = Red).getModifiers
        //    //};

        //    //Point3d malefemalescale = new Point3d(new Point(0.2f, 0.2f), 0.2f);
        //    //Point3d malefemalescaleinv = new Point3d(new Point(-0.2f, 0.2f), 0.2f);
        //    //string malefemalevelocity = "3";

        //    //ShapeVO maleStand = new ShapeVO(new ShapeVOId("maleStand"), new ShapeTypeVOId(ItemUC.ShapeTypeSprite))
        //    //{ // cube.Id
        //    //    TypeModifiers = "maleandfemalefinal.png, 1,1, 0,5, 35,75",
        //    //    //TypeModifiers = "maleandfemalefinal.png",
        //    //    Position = new Point3d(new Point(-35 / 2, -80), 0),
        //    //    Scale = malefemalescale
        //    //};

        //    //ShapeVO maleWalkSouth = new ShapeVO(new ShapeVOId("maleWalkSouth"), new ShapeTypeVOId(ItemUC.ShapeTypeSprite))
        //    //{
        //    //    TypeModifiers = "maleandfemalefinal.png, 4," + malefemalevelocity + ", 70,0, 105,80,  105,0, 140,80,  140,0, 175,80,  175,0, 210,80",
        //    //    Position = new Point3d(new Point(-35 / 2, -80), 0),
        //    //    Scale = malefemalescale
        //    //};

        //    //ShapeVO maleWalkEast = new ShapeVO(new ShapeVOId("maleWalkEast"), new ShapeTypeVOId(ItemUC.ShapeTypeSprite))
        //    //{
        //    //    TypeModifiers = "maleandfemalefinal.png, 4," + malefemalevelocity + ", 70,0, 105,80,  105,0, 140,80,  140,0, 175,80,  175,0, 210,80",
        //    //    Position = new Point3d(new Point(-35 / 2, -80), 0),
        //    //    Scale = malefemalescaleinv
        //    //};


        //    //ShapeVO maleWalkWest = new ShapeVO(new ShapeVOId("maleWalkNorth"), new ShapeTypeVOId(ItemUC.ShapeTypeSprite))
        //    //{
        //    //    TypeModifiers = "maleandfemalefinal.png, 4," + malefemalevelocity + ", 0,80, 35,160,  35,80, 70,160,  70,80, 105,160,  105,80, 140,160",
        //    //    Position = new Point3d(new Point(-35 / 2, -80), 0),
        //    //    Scale = malefemalescale
        //    //};

        //    //ShapeVO maleWalkNorth = new ShapeVO(new ShapeVOId("maleWalkWest"), new ShapeTypeVOId(ItemUC.ShapeTypeSprite))
        //    //{
        //    //    TypeModifiers = "maleandfemalefinal.png, 4," + malefemalevelocity + ", 0,80, 35,160,  35,80, 70,160,  70,80, 105,160,  105,80, 140,160",
        //    //    Position = new Point3d(new Point(-35 / 2, -80), 0),
        //    //    Scale = malefemalescaleinv
        //    //};


        //    //#endregion

        //    //#region Views

        //    //ViewVO malePeasantView = new ViewVO(new ViewVOId("malePeasentView"));

        //    //malePeasantView.AddShapeToState(IViewVOConstants.DefaultState, maleStand);

        //    //malePeasantView.AddState(MyFacade.Instance.standSouthState);
        //    //malePeasantView.AddShapeToState(MyFacade.Instance.standSouthState, maleStand);
        //    //malePeasantView.AddState(MyFacade.Instance.walkSouthState);
        //    //malePeasantView.AddShapeToState(MyFacade.Instance.walkSouthState, maleWalkSouth);
        //    //malePeasantView.AddState(MyFacade.Instance.walkEastState);
        //    //malePeasantView.AddShapeToState(MyFacade.Instance.walkEastState, maleWalkEast);
        //    //malePeasantView.AddState(MyFacade.Instance.walkWestState);
        //    //malePeasantView.AddShapeToState(MyFacade.Instance.walkWestState, maleWalkWest);
        //    //malePeasantView.AddState(MyFacade.Instance.walkNorthState);
        //    //malePeasantView.AddShapeToState(MyFacade.Instance.walkNorthState, maleWalkNorth);

        //    ////predadorView.AddState(workSouthState);
        //    ////predadorView.AddShapeToState(walkSouthState, maleWorkSouth);
        //    ////predadorView.AddState(workEastState);
        //    ////predadorView.AddShapeToState(workEastState, maleWorkEast);
        //    ////predadorView.AddState(workWestState);
        //    ////predadorView.AddShapeToState(workWestState, maleWorkWest);
        //    ////predadorView.AddState(workNorthState);
        //    ////predadorView.AddShapeToState(workNorthState, maleWorkNorth);
        //    //ViewVO houseView = new ViewVO(new ViewVOId("houseView"));
        //    //houseView.Shapes[IViewVOConstants.DefaultState].Add(redCube.Id, redCube);

        //    //ViewVO fieldView = new ViewVO(new ViewVOId("fieldView"));
        //    //fieldView.Shapes[IViewVOConstants.DefaultState].Add(yellowCube.Id, yellowCube);

        //    //ViewVO grassView = new ViewVO(new ViewVOId("grassView"));
        //    //grassView.Shapes[IViewVOConstants.DefaultState].Add(greenSphere.Id, greenSphere);

        //    //ViewVO treeView = new ViewVO(new ViewVOId("treeView"));
        //    //treeView.Shapes[IViewVOConstants.DefaultState].Add(brownCube.Id, brownCube);
        //    //treeView.Shapes[IViewVOConstants.DefaultState].Add(arvoreGreenSphere.Id, arvoreGreenSphere);

        //    //game.Views.Add(malePeasantView.Id, malePeasantView);
        //    //game.Views.Add(houseView.Id, houseView);
        //    //game.Views.Add(fieldView.Id, fieldView);
        //    //game.Views.Add(grassView.Id, grassView);
        //    //game.Views.Add(treeView.Id, treeView);
        //    //#endregion

        //    //#region Types
        //    ////Add ThingTypes
        //    //PeasantTypeVO peasantMaleTypeVO = new PeasantTypeVO(MyFacade.Instance.peasantMaleTypeId, malePeasantView.Id)
        //    //{
        //    //    Velocity = 0.1f

        //    //};
        //    //game.ThingTypes.Add(peasantMaleTypeVO.Id, peasantMaleTypeVO);

        //    //ThingTypeVO<int> houseTypeVO = new BuildingTypeVO(MyFacade.Instance.houseTypeId, houseView.Id); ;
        //    //game.ThingTypes.Add(houseTypeVO.Id, houseTypeVO);

        //    //ThingTypeVO<int> fieldTypeVO = new ThingTypeVO<int>(MyFacade.Instance.fieldTypeId, fieldView.Id); ;
        //    //game.ThingTypes.Add(fieldTypeVO.Id, fieldTypeVO);

        //    ////PeasantTypeVO peasantFemaleTypeVO = new PeasantTypeVO(peasantFemaleType, fieldView.Id)
        //    ////{
        //    ////    Velocity = 0.1f
        //    ////};
        //    ////game.ThingTypes.Add(peasantFemaleTypeVO.Id, peasantFemaleTypeVO);

        //    //ThingTypeVO<int> grassType = new ThingTypeVO<int>(MyFacade.Instance.grassTypeId, grassView.Id)
        //    //{
        //    //};
        //    //game.ThingTypes.Add(grassType.Id, grassType);

        //    //ThingTypeVO<int> treeType = new ThingTypeVO<int>(MyFacade.Instance.treeTypeId, treeView.Id)
        //    //{
        //    //};
        //    //game.ThingTypes.Add(treeType.Id, treeType);
        //    //#endregion

        //    ///// Level
        //    //ILevelVO<int> level = new LevelVO<int>(new LevelVOId("MyLevel"));

        //    ///// World
        //    /////     Map
        //    //MapVO map = new MapVO("MyMap");
        //    //map.Size = new Point(150, 150);
        //    //map.CreateTiles();
        //    //map.GenerateHeightsFromPerlin(new Point(0, 0), 0.01f, 1, 1, 1, 1);
        //    ////map.Tiles[5][5].Height = 10;
        //    //map.Camera.Position = new Point(75, 75);
        //    //map.Camera.Scale = 2.25f;

        //    //IWorldVO<int> MyWorld = new WorldVO<int>(new WorldVOId("MyWorld"))
        //    //{
        //    //    Map = map,
        //    //};

        //    //#region Items
        //    /////     Items

        //    ////world.Items.Add(new PeasantVO(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), peasantMaleTypeVO.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(75, 76), 0),
        //    ////    State = walkEastState
        //    ////});
        //    ////world.Items.Add(new PeasantVO(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), peasantMaleTypeVO.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(75, 77), 0),
        //    ////    State = walkWestState
        //    ////});
        //    ////world.Items.Add(new PeasantVO(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), peasantMaleTypeVO.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(75, 78), 0),
        //    ////    State = walkNorthState
        //    ////});
        //    ////world.Items.Add(new PeasantVO(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), peasantMaleTypeVO.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(75, 79), 0),
        //    ////    State = walkSouthState
        //    ////});

        //    ////erva
        //    ////world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), grassType.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(80, 80), 0),
        //    ////});

        //    ////arvore
        //    ///*world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
        //    //{
        //    //    Position = new MapEngine.Point3d(new Point(75, 74), 0),
        //    //});*/
        //    ////world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(50, 50), 0),
        //    ////});
        //    ////world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(50, 100), 0),
        //    ////});
        //    ////world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(100, 50), 0),
        //    ////});
        //    ////world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(100, 100), 0),
        //    ////});
        //    ////world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(25, 75), 0),
        //    ////});
        //    ////world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(75, 25), 0),
        //    ////});
        //    ////world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(125, 75), 0),
        //    ////});
        //    ////world.Items.Add(new ThingVO<int>(new ThingVOId<int>(SilverlightModelFacade.Instance.CreateNewId()), treeType.Id)
        //    ////{
        //    ////    Position = new MapEngine.Point3d(new Point(75, 125), 0),
        //    ////});
        //    //#endregion

        //    //level.Worlds.Add(MyWorld);

        //    ///// World
        //    /////     Map
        //    //MapVO menuMap = new MapVO("MenuMap");
        //    //menuMap.Size = new Point(3, 3);
        //    //menuMap.CreateTiles();
        //    ////menuMap.GenerateHeightsFromPerlin(new Point(0, 0), 0.01f, 1, 1, 1, 1);
        //    ////map.Tiles[5][5].Height = 10;
        //    //menuMap.Camera.Position = new Point(1, 1);
        //    //menuMap.Camera.Scale = 1f;

        //    //IWorldVO<int> MenuWorld = new WorldVO<int>(new WorldVOId("MenuWorld"))
        //    //{
        //    //    Map = menuMap,
        //    //};

        //    //#region Items

        //    //#endregion

        //    //level.Worlds.Add(MenuWorld);

        //    //game.Levels.Add(level.Id, level);
        //    //game.LevelOrder.Add(0, level.Id);
        //    return game;
        //}
    }
}
